﻿$(document).ready(function () {
    //RATING
    //-----------------------------------------------------------------------------------------
    // Variable to set the duration of the animation
    var animationTime = 500;

    // Variable to store the colours
    var colours = ["bd2c33", "e49420", "ecdb00", "3bad54", "1b7db9"];

    // Add rating information box after rating
    var ratingInfobox = $("<div />")
		    .attr("id", "ratinginfo")
		    .insertAfter($("#rating"));

    //GET GLOBAL RATING
    //-----------------------------------------------------------------------------------------
    $.ajax({
        type: "POST",
        url: "Art.aspx/GetStarsAllUsers",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $("#globelRating").html(msg.d)
                .append();
            colourizeGeneralRatings(msg.d);
            error: AjaxFailed
        }
    });
    $.ajax({
        type: "POST",
        url: "Art.aspx/GetStarsUser",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            colourizeRatings(msg.d);
            error: AjaxFailed
        }
    });
    var colourizeGeneralRatings = function (nrOfRatings) {
        $("#globelRating").css("backgroundColor", "#" + colours[parseInt(nrOfRatings) - 1]);

    };
    // Function to colorize the right ratings
    var colourizeRatings = function (nrOfRatings) {
        $("#rating li a").each(function () {
            if ($(this).parent().index() <= nrOfRatings) {
                $(this).stop().animate({ backgroundColor: "#" + colours[nrOfRatings] }, animationTime);
            }
        });
    };
    //
    var voted = false;
    var index = 0;

    $("#rating li a").hover(function () {

        // Empty the rating info box and fade in
        ratingInfobox
			    .empty()
			    .stop()
			    .animate({ opacity: 1 }, animationTime);

        // Add the text to the rating info box
        //if (!voted) {
        $("<p />")
			    .html($(this).html())
			    .appendTo(ratingInfobox);

        // Call the colourize function with the given index            
        colourizeRatings($(this).parent().index());
        /*} else {
        $("<p />")
        .html("Thank you for voting")
        .appendTo(ratingInfobox);
        colourizeRatings(index);
        }*/
    }, function () {

        // Fade out the rating information box
        ratingInfobox
			    .stop()
			    .animate({ opacity: 0 }, animationTime);

        // Restore all the rating to their original colours
        $("#rating li a").stop().animate({ backgroundColor: "#333" }, animationTime);

        //Get Global rating
        $.ajax({
            type: "POST",
            url: "Art.aspx/GetStarsUser",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                colourizeRatings(msg.d);
                error: AjaxFailed
            }
        });
    });

    // Prevent the click event and show the rating
    $("#rating li a").click(function (e) {
        //if (!voted) {
        /*e.preventDefault();
        alert("You voted on item number " + ($(this).parent().index() + 1));*/
        var vote = $(this).parent().index() + 1;
        e.preventDefault();
        voted = true;
        $.ajax({
            type: "POST",
            url: "Art.aspx/SetStars",
            data: "{'stars': '" + vote + "'}",

            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                //returns the right index that was voted
                voted = true;
                index = msg.d;
                $.ajax({
                    type: "POST",
                    url: "Art.aspx/GetStarsAllUsers",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        $("#globelRating").html(msg.d)
                .append();
                        //$("{#generalRating:nth-child(msg.d)}").child(msg.d)
                        //$("{'#generalRating:nth-child('" + msg.d + "')}").addClass("showRating");
                        colourizeGeneralRatings(msg.d);
                        error: AjaxFailed
                    }
                });
                error: AjaxFailed

            }
        });
        /*} else {
        alert("You have already voted!");
        }*/
    });

    //RATING
    /*var rating = $("#userRating").val();
    //var rating = '4'; 

    switch (rating) {
    case '1':
    $("#rating_1").attr('src', '../../../../styles/assets/images/cambell.png');
    break;
    case '2':
    $("#rating_1").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_2").attr('src', '../../../../styles/assets/images/cambell.png');
    break;
    case '3':
    $("#rating_1").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_2").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_3").attr('src', '../../../../styles/assets/images/cambell.png');
    break;
    case '4':
    $("#rating_1").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_2").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_3").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_4").attr('src', '../../../../styles/assets/images/cambell.png');
    break;
    case '5':
    $("#rating_1").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_2").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_3").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_4").attr('src', '../../../../styles/assets/images/cambell.png');
    $("#rating_5").attr('src', '../../../../styles/assets/images/cambell.png');
    break;
    }*/
});